package com.mtnz.njk.GexinUtil;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;

/**
 * @Description: 向个人推送信息
 * @Author: FBL
 * @CreateDate: 2019\8\28 0028 15:22
 * @Version: 1.0
 */
public class PushToSingle {
    public void main(String[] args) throws Exception{
        pushToSingle("1974064e08cdb26982f5974cdc67ad82","","",2,"透传消息内容",2);
    }

    /**
     * 向个人推送信息
     *
     * @param cId                 clientId 手机的唯一标识
     * @param title               通知栏标题
     * @param text                通知栏内容
     * @param transmissionType    后续动作 ：1 立即启动APP，2：客户端收到消息后需要自行处理
     * @param transmissionContent 透传消息内容
     * @param tranStatus          消息类型 1 通知消息 2 透传消息
     * @throws Exception
     */
    public void pushToSingle(String cId, String title, String text, Integer transmissionType, String transmissionContent,Integer tranStatus) throws Exception {
        IGtPush push = new IGtPush(GexinConfig.URL, GexinConfig.APP_KEY, GexinConfig.MASTER_SECRET);
        if(tranStatus == 1){
            NotificationTemplate template = getNotificationTemplate(title, text, transmissionType, transmissionContent);
            SingleMessage message = new SingleMessage();
            message.setOffline(true);
            // 离线有效时间，单位为毫秒
            message.setOfflineExpireTime(24 * 3600 * 1000);
            message.setData(template);
            // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
            message.setPushNetWorkType(0);
            Target target = new Target();
            target.setAppId(GexinConfig.APP_ID);
            target.setClientId(cId);
            IPushResult ret = null;
            try {
                ret = push.pushMessageToSingle(message, target);
            } catch (RequestException e) {
                e.printStackTrace();
                ret = push.pushMessageToSingle(message, target, e.getRequestId());
            }
            if (ret != null) {
                System.out.println(ret.getResponse().toString());
            } else {
                System.out.println("服务器响应异常");
            }
        }

        if(tranStatus == 2){
            TransmissionTemplate transmissionTemplate = getTransmissionTemplate(transmissionType, transmissionContent);
            SingleMessage message = new SingleMessage();
            message.setOffline(true);
            // 离线有效时间，单位为毫秒
            message.setOfflineExpireTime(24 * 3600 * 1000);
            message.setData(transmissionTemplate);
            // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
            message.setPushNetWorkType(0);
            Target target = new Target();
            target.setAppId(GexinConfig.APP_ID);
            target.setClientId(cId);
            IPushResult ret = null;
            try {
                ret = push.pushMessageToSingle(message, target);
            } catch (RequestException e) {
                e.printStackTrace();
                ret = push.pushMessageToSingle(message, target, e.getRequestId());
            }
            if (ret != null) {
                System.out.println(ret.getResponse().toString());
            } else {
                System.out.println("服务器响应异常");
            }
        }
    }

    /**
     * 通知消息模板
     *
     * @param title
     * @param text
     * @param transmissionType
     * @param transmissionContent
     * @return
     */
    public static NotificationTemplate getNotificationTemplate(String title, String text, Integer transmissionType, String transmissionContent) {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(GexinConfig.APP_ID);
        template.setAppkey(GexinConfig.APP_KEY);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(title);
        style.setText(text);
        // 配置通知栏图标
        style.setLogo("");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        style.setChannel("通知渠道id");
        style.setChannelName("通知渠道名称");
        style.setChannelLevel(3); //设置通知渠道重要性
        template.setStyle(style);

        template.setTransmissionType(transmissionType);  // 透传消息接受方式设置，1：立即启动APP，2：客户端收到消息后需要自行处理
        template.setTransmissionContent(transmissionContent);
        return template;
    }

    /**
     * 透传消息模板
     *
     * @param transmissionType
     * @param transmissionContent
     * @return
     */
    public static TransmissionTemplate getTransmissionTemplate(Integer transmissionType, String transmissionContent) {
        TransmissionTemplate transmissionTemplate = new TransmissionTemplate();

        transmissionTemplate.setAppId(GexinConfig.APP_ID);
        transmissionTemplate.setAppkey(GexinConfig.APP_KEY);

        transmissionTemplate.setTransmissionType(transmissionType);
        transmissionTemplate.setTransmissionContent(transmissionContent);

        return transmissionTemplate;

    }


}
